__author__ = 'biwangshen_sx'

import requests
import logging

LOGGER = logging.getLogger(__name__)

from .endpoints import urls

def gen_url(name, host=None):
    if host is None:
        backend = 'http://gamecloud-test.qiyi.domain:81'
    else:
        backend = host
    url = urls[name].rstrip('/')
    LOGGER.debug('get_url: %s', url)
    return backend + url


def _get_user_profile(user_name):
    url = gen_url('users_q')
    r = requests.get(url, params={'user_name': user_name})
    LOGGER.debug('_get_user_profile: %s ,content: %s', r.status_code , r.content)
    if r.ok:
        data = r.json()
        return data['status'], data['message']
    else:
        return 1, r.content or r.reason


def register_user(user_name, show_name, email, is_admin=0):
    url = gen_url('users_add')
    values = {
        'user_name': user_name,
        'show_name': show_name,
        'email': email,
    }
    r = requests.post(url, data=values)
    if r.ok:
        LOGGER.debug('_get_user_profile success:content: %s', r.content)
        r_data = r.json()
        return r_data['status'], r_data['message']
    else:
        LOGGER.error(r.content or r.reason)
        return r.status_code, r.content or r.reason
